package cz.muni.fi.pa165.machineryrental.service.impl;

import cz.muni.fi.pa165.machineryrental.converters.RevisionDTOConverter;
import cz.muni.fi.pa165.machineryrental.dao.RevisionDAO;
import cz.muni.fi.pa165.machineryrental.dto.RevisionDTO;
import cz.muni.fi.pa165.machineryrental.entities.Revision;
import cz.muni.fi.pa165.machineryrental.service.RevisionService;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Implementation of RevisionService
 *
 * @author Martin Klvana, 395893
 */
@Service("RevisionService")
public class RevisionServiceImpl implements RevisionService {

    @Autowired
    private RevisionDAO revisionDAO;

    @Autowired
    public RevisionServiceImpl(RevisionDAO revisionDAO) {
        if (revisionDAO == null) {
            throw new IllegalArgumentException("Given RevisionDAO cannot be null.");
        }
        this.revisionDAO = revisionDAO;
    }

    @Override
    @Transactional(readOnly = true)
    public RevisionDTO getById(long id) {
        if (id < 0) {
            throw new IllegalArgumentException("negative id");
        }

        Revision revision = revisionDAO.getById(id);
        return RevisionDTOConverter.toDTO(revision);
    }

    @Override
    @Transactional
    public void create(RevisionDTO revision) {
        if (revision == null) {
            throw new IllegalArgumentException("null revision");
        }
        Revision entity = RevisionDTOConverter.toEntity(revision);
        revisionDAO.create(entity);
    }

    @Override
    @Transactional
    public void update(RevisionDTO revision) {
        if (revision == null) {
            throw new IllegalArgumentException("null revision");
        }
        if (revision.getId() <= 0) {
            throw new IllegalArgumentException("revision unexists");
        }
        Revision entity = revisionDAO.getById(revision.getId());
        entity.setDateOfRevision(revision.getDateOfRevision());
        entity.setDescription(revision.getDescription());

        revisionDAO.update(entity);
    }

    @Override
    @Transactional
    public void remove(RevisionDTO revision) {
        if (revision == null) {
            throw new IllegalArgumentException("null revision");
        }
        if (revision.getId() <= 0) {
            throw new IllegalArgumentException("revision unexists");
        }

        Revision entity = revisionDAO.getById(revision.getId());
        revisionDAO.remove(entity);
        revision.setId(0);
    }

    @Override
    @Transactional(readOnly = true)
    public List<RevisionDTO> findAll() {
        List<Revision> revisions = revisionDAO.findAll();

        List<RevisionDTO> result = new ArrayList<RevisionDTO>();
        for (Revision r : revisions) {
            result.add(RevisionDTOConverter.toDTO(r));
        }
        return result;
    }

    @Override
    @Transactional(readOnly = true)
    public List<RevisionDTO> findByInterval(Date startDate, Date endDate) {
        List<Revision> revisions = revisionDAO.findByInterval(startDate, endDate);

        List<RevisionDTO> result = new ArrayList<RevisionDTO>();
        for (Revision r : revisions) {
            result.add(RevisionDTOConverter.toDTO(r));
        }
        return result;
    }
}
